class Player {
    constructor(Platform) {
        if (Platform instanceof String) {
            throw "Platform must a string.";
        }

        this.Run = false;
        this.xmlhttp = new XMLHttpRequest();
        this.aimCode = "" //目标机器的连接Code
        this.MarginTime = 3 //间隔超过MarginTime秒时进行更新。
        this.UploadTime = 500 //每隔UploadTime毫秒进行上传。
        this.delayTime = 0.8 //延时补偿(秒)
        this.DisControl = false;
        this.PlatformEnum = { "iQIY": 0 };
        if (this.PlatformEnum[Platform] === undefined) {
            throw Platform + " is not a support video platform.";
        } else {
            switch (this.PlatformEnum[Platform]) {
                case 0:
                    this.Player = document.querySelector('[data-player-hook="container"]').firstElementChild;

                    var TmpDiv = document.createElement("div");
                    TmpDiv.style = "text-align: center;color: #222;font-size: 20px;"

                    var TmpSpan = document.createElement("span");
                    TmpSpan.style = "margin-right:20px;"
                    TmpSpan.innerHTML = '本机Code=><a id="Code">请启动后获取</a>&nbsp<button id="ControlButton" onclick="player.ChangeStatus()">启动</button>'
                    TmpDiv.appendChild(TmpSpan)

                    this.TmpSpan1 = document.createElement("span");
                    this.TmpSpan1.style = "margin-right:20px;"
                    this.TmpSpan1.innerHTML = '目标Code=><input style="width:80px" id="aimCodeInput" maxlength="6">&nbsp<button onclick="player.aimCode=document.getElementById(\'aimCodeInput\').value">连接</button>'
                    TmpDiv.appendChild(this.TmpSpan1)

                    this.TmpSpan2 = document.createElement("span");
                    this.TmpSpan2.style = "margin-right:20px;"
                    this.TmpSpan2.innerHTML = 'From=><a id="FromCode">未受控</a>&nbsp<button onclick="player.DisControl=true;" id="fromcodeControl" disabled="disabled">断开</button>'
                    TmpDiv.appendChild(this.TmpSpan2)

                    var TmpSpan3 = document.createElement("span");
                    TmpSpan3.innerHTML = '延时补偿=><input style="width:80px" id="delayInput" value=\"' + String(this.delayTime) + '\" type="text" onkeyup="checkInputIntFloat(this);">&nbsp<button onclick="player.delayTime=parseFloat(document.getElementById(\'delayInput\').value);" id="delayControl">确认</button>'
                    TmpDiv.appendChild(TmpSpan3)

                    var NavDiv = document.getElementById("flashOutter");
                    NavDiv.parentNode.insertBefore(TmpDiv, NavDiv.nextSibling);

                    break;
            }
        }
    }
    get currentTime() {
        return this.Player.currentTime;
    }
    set currentTime(Time) {
        if (Player.currentTime != Time) {
            this.Player.currentTime = Time;
        }
    }
    set SetPoster(ImgUrl) {
        this.Player.poster = ImgUrl;
    }
    ChangeStatus() {
        if (this.Run) {
            if (typeof (this.Interval) != "undefined") {
                clearInterval(this.Interval);
            }
            document.getElementById("ControlButton").innerText = "启动";
            document.getElementById("Code").innerText = "请启动后获取";
        } else {
            document.getElementById("Code").innerText = "正在连接中..";
            var FirstTime = true;
            this.Interval = setInterval(() => {
                var Url = "https://49.232.23.124:9710/";
                // var Url = "https://127.0.0.1:9710/";
                var Mess = {};
                Mess["paused"] = this.Player.paused;
                Mess["videotime"] = this.currentTime + this.delayTime;
                Mess["aimcode"] = this.aimCode;
                Mess["discontrol"] = this.DisControl;
                this.DisControl = false;
                this.aimCode = "";
                if (this.Code) {
                    Mess["code"] = this.Code;
                }
                this.xmlhttp.open("POST", Url, true);
                this.xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
                this.xmlhttp.onreadystatechange = () => {
                    if (this.xmlhttp.readyState == 4) {
                        if (this.xmlhttp.status == 200) {
                            var Res = JSON.parse(this.xmlhttp.responseText);
                            console.log(Res);
                            if (Res["code"]) {
                                this.Code = Res["code"];
                                document.getElementById("Code").innerText = this.Code;
                            } else if (FirstTime) {
                                document.getElementById("Code").innerText = this.Code;
                                FirstTime = false;
                            }
                            if (typeof (Res["paused"]) != "undefined") {
                                console.log(Res["paused"])
                                if (Res["paused"]) {
                                    this.pause()
                                    if (typeof (Res["newtime"]) != "undefined") {
                                        this.currentTime = Res["newtime"];
                                    }
                                } else {
                                    console.log("Play")
                                    this.play()
                                }
                            }
                            if (typeof (Res["newtime"]) != "undefined") {
                                if (Math.abs(Res["newtime"] - this.currentTime) > this.MarginTime) {
                                    this.currentTime = Res["newtime"];
                                }
                            }
                            if (typeof (Res["fromcode"]) != "undefined") {
                                document.getElementById("fromcodeControl").removeAttribute("disabled");
                                document.getElementById("FromCode").innerText = Res["fromcode"];
                            } else {
                                document.getElementById("fromcodeControl").setAttribute("disabled", "disabled");
                                document.getElementById("FromCode").innerText = "未受控";
                            }
                            for (var ind = 0; ind < Res["Mess"].length; ++ind) {
                                alert(Res["Mess"][ind]);
                            }
                        }
                    }
                }
                this.xmlhttp.send(JSON.stringify(Mess));
            }, this.UploadTime);
            document.getElementById("ControlButton").innerText = "停止";
        }
        this.Run = !this.Run
    }

    SyncAimTime(aimCode) {
        this.pause();
        this.aimCode = aimCode;
    }
    url() {
        return window.location.href;
    }
    pause() {
        this.Player.pause();
    }
    play() {
        this.Player.play();
    }
    sync() {
        this.Player.oncanplaythrough = (event) => {
            console.log(event);
        };
    }
}

player = new Player("iQIY")